<template>
  <div class="dialog-box" style="position: relative">
    <el-scrollbar class="custom-scroll content-box">
      <el-form
        ref="editProjectTemplateRef"
        :model="formData"
        :size="defaultFormItemSize"
        :rules="rules"
        label-width="120px"
        label-position="right"
        @submit.native.prevent
      >
        <el-row :gutter="16">
          <el-col :span="24">
            <el-form-item label="模板名称" prop="ProjectTemplate.name">
              <el-input
                class="input-item"
                v-model="formData.ProjectTemplate.name"
                type="text"
                placeholder=""
                :clearable="true"
                :show-word-limit="false"
                maxlength="36"
              />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="模板描述" prop="ProjectTemplate.description">
              <el-input
                class="input-item"
                v-model="formData.ProjectTemplate.description"
                type="textarea"
                placeholder=""
                :clearable="true"
                :show-word-limit="false"
                maxlength="512"
              />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-scrollbar>
    <el-row class="footer-box" type="flex" justify="end" align="middle">
      <el-button :size="defaultFormItemSize" @click="onCancel()">取消</el-button>
      <el-button :size="defaultFormItemSize" type="primary" @click="onSubmitProjectTemplateClick()"
        >保存</el-button
      >
    </el-row>
  </div>
</template>

<script>
import rules from '@/utils/validate';
import {
  stringCase,
  treeDataTranslate,
  findItemFromList,
  findTreeNodePath,
  findTreeNode
} from '@/utils';
import { uploadMixin, statsDateRangeMixin, cachePageMixin } from '@/core/mixins';
import { DropdownWidget, TableWidget, UploadWidget, ChartWidget } from '@/utils/widget.js';
import { LayerDefineController, ProjectTemplateController, DictionaryController } from '@/api';

export default {
  name: 'copyProjectTemplate',
  props: {
    id: {
      default: undefined
    },
    subPage: {
      type: [Number, String, Boolean],
      default: 0
    },
    saveOnSubmit: {
      type: Boolean,
      default: true
    },
    rowData: {
      type: Object
    }
  },
  mixins: [uploadMixin, statsDateRangeMixin, cachePageMixin],
  data() {
    return {
      formData: {
        ProjectTemplate: {
          // 模板ID
          id: undefined,
          // 模板名称
          name: undefined,
          // 模板描述
          description: undefined,
          // 是否默认
          isDefault: undefined,
          isDatasourceInit: false
        }
      },
      editProjectTemplate: {
        isInit: false
      },
      rules: {
        'ProjectTemplate.name': [{ required: true, message: '请输入模板名称', trigger: 'blur' }],
        'ProjectTemplate.description': []
      }
    };
  },
  methods: {
    onCancel(isSuccess = false, data = null) {
      if (this.observer != null) {
        this.observer.cancel(isSuccess, data);
      }
    },
    /**
     * 重置过滤值
     */
    resetEditProjectTemplate() {
      this.refreshEditProjectTemplate();
    },
    /**
     * 重置所有过滤值
     */
    resetFilter() {
      this.resetEditProjectTemplate();
    },
    /**
     * 保存
     */
    onSubmitProjectTemplateClick() {
      this.$refs.editProjectTemplateRef.validate(valid => {
        if (!valid) return;
        // 级联操作
        if (!this.saveOnSubmit) {
          let formData = {
            ...this.formData.ProjectTemplate
          };
          this.onCancel(true, formData);
          return;
        }
        let params = {
          projectTemplateDto: {
            id: this.formData.ProjectTemplate.id,
            name: this.formData.ProjectTemplate.name,
            description: this.formData.ProjectTemplate.description,
            isDefault: this.formData.ProjectTemplate.isDefault
          }
        };

        let httpCall = this.isEdit
          ? ProjectTemplateController.update
          : ProjectTemplateController.add;
        httpCall(this, params)
          .then(res => {
            this.$message.success('保存成功');
            this.onCancel(true);
          })
          .catch(e => {});
      });
    },
    loadProjectTemplateData() {
      return new Promise((resolve, reject) => {
        if (!this.isEdit) {
          resolve();
          return;
        }
        if (!this.saveOnSubmit && this.rowData != null) {
          this.formData.ProjectTemplate = JSON.parse(JSON.stringify(this.rowData));
          resolve();
          return;
        }
        let params = {
          id: this.id
        };
        ProjectTemplateController.view(this, params)
          .then(res => {
            this.formData.ProjectTemplate = { ...res.data, isDatasourceInit: true };
            resolve();
          })
          .catch(e => {
            reject(e);
          });
      });
    },
    onUploadError(e, file, fileList) {
      this.$message.error('文件上传失败');
    },
    onUploadLimit(files, fileList) {
      this.$message.error('已经超出最大上传个数限制');
    },
    refreshEditProjectTemplate(reloadData = false) {},
    formInit() {
      this.loadProjectTemplateData()
        .then(res => {
          if (this.isEdit) this.refreshEditProjectTemplate();
        })
        .catch(e => {});
    }
  },
  computed: {
    isEdit() {
      return this.saveOnSubmit ? this.id != null : this.rowData != null;
    }
  },
  mounted() {
    // 初始化页面数据
    this.formInit();
  }
};
</script>
